<template>
  <div>
    <div class="adminInput">
      <div class="admin-in">
        <el-input
          v-model="input"
          @input="inputText"
          placeholder="请输入要查找用户名"
        ></el-input>
      </div>
    </div>

    <el-table v-loading="loading" :data="tableData" border style="width: 100%">
      <el-table-column prop="userId" label="用户id" width="90">
      </el-table-column>
      <el-table-column prop="userName" label="用户名" width="200">
      </el-table-column>
      <el-table-column prop="userNum" label="用户账号" width="200">
      </el-table-column>
      <el-table-column
        prop="userPwd"
        label="用户密码"
        width="230"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column prop="userEmail" label="用户邮箱" width="280">
      </el-table-column>
      <el-table-column prop="userState" label="状态" width="100">
      </el-table-column>
      <el-table-column label="操作" width="100" fixed="right">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="text" size="small"
            >修改状态</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <div class="admin-fenye">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :page-size="6"
        :currentPage="curPage"
        @current-change="page"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { request } from "../../network/request";

export default {
  name: "adminUser",
  created() {
    this.getMessage();
  },
  methods: {
    getMessage() {
      if (this.$route.query.page != undefined) {
        this.curPage = this.$route.query.page;
      }
      request({
        url: "/user/queryUserList",
        method: "post",
        params: {
          pageNum: this.curPage,
        },
      })
        .then((res) => {
          this.tableData = res.data.data.list;
          this.total = res.data.data.total;
          this.loading = false;
        })
        .catch((err) => {
          console.log(err);
        });
    },

    handleClick(row) {
      if (row.userState == 0) {
        this.state = 1;
      } else {
        this.state = 0;
      }
      request({
        url: "/user/deleteUser",
        method: "post",
        params: {
          userId: row.userId,
          userState: this.state,
        },
      })
        .then(() => {
          this.$alert("ID为 : " + row.userId + "的用户 状态修改成功", "提示", {
            confirmButtonText: "确定",
            callback: () => {
              location.reload();
            },
          });
        })
        .catch((err) => {
          console.log(err);
        });
      console.log(row.userState);
    },
    page(currentPage) {
      this.curPage = currentPage;
      this.loading = true;
      this.$router.push({
        path: "administration",
        query: {
          page: currentPage,
        },
      });
      this.getMessage();
    },
    inputText() {
      this.curPage = 1;
      this.$router.push({
        path: "administration",
        query: {
          page: 1,
        },
      });
      request({
        url: "/user/queryLikeUserList",
        method: "post",
        params: {
          likeKeywords: this.input,
          pageNum: this.curPage,
        },
      })
        .then((res) => {
          this.tableData = res.data.data.list;
          this.total = res.data.data.total;
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },

  data() {
    return {
      tableData: "",
      input: "",
      curPage: 1,
      total: 0,
      state: "",
      loading: true,
    };
  },
};
</script>

<style>
.adminInput {
  display: flex;
  margin-top: 0.15rem;
  margin-bottom: 0.3rem;
  font-size: 0.1rem;
}

.admin-in {
  width: 2.5rem;
  margin-right: 0.2rem;
}

.admin-fenye {
  text-align: center;
  font-size: 0.1rem;
  margin-top: 0.3rem;
}
</style>